
hide_shop_sprites:
	ld		hl,shop_fire_struct+12
	ld		a,193
	ld		b,6*2
	ld		de,4
hide_shop_sprites_loop:
	ld		[hl],a
	add		hl,de
	djnz	hide_shop_sprites_loop


	ld		a,193
	ld		bc,4*3
	ld		hl,SPRATR+(4*12)
	call	0x56

	ret

shop_sprites_attrb:
	db	#8f,#70,#88,#0f
	db	#8f,#e8,#8c,#0e
	db	#8f,#70,#84,#06

set_shop_sprites:
	ld	hl,shop_sprites_attrb
	ld	de,SPRATR+(4*12)
	ld	bc,12
	call	0x05c

	ret


SHOP_FIRE_PERIOD	equ		3

init_shop_fire_fx:

	ld		a,SHOP_FIRE_PERIOD					;period
	ld		hl,shop_fire_struct
	ld		[hl],a
	ld		bc,5
	ld		d,h
	ld		e,l
	inc		de
	ldir	
	
	ld		a,0					     ;default index
	ld		hl,shop_fire_struct+6
	ld		[hl],a
	ld		bc,5
	ld		d,h
	ld		e,l
	inc		de
	ldir	

	ld		hl,shop_fire_initial_values
	ld		de,shop_fire_struct+12
	ld		bc,2*6*4
	ldir

	ret




;a current index
;ret
;a next index
next_fire_scene:
	cp		FIRE_SCENE_LIST_SIZE
	jr		z,next_fire_scene_reset
	inc		a
	ret
next_fire_scene_reset:
	xor		a
	ret






update_shop_fires_fx:
	ld		b,6
update_shop_fires_fx_loop:
	push	bc

	dec		b
	ld		a,b
	call	update_single_fire_fx

	pop		bc
	djnz	update_shop_fires_fx_loop

	ret




;a fire index
update_single_fire_fx:
	ld		b,a						;save a for later
	ld		a,r
	cp		10
	ret		c
	
	ld		a,b						;restore b
	ld		hl,shop_fire_struct
	ld		b,0
	ld		c,a
	add		hl,bc
	dec		[hl]
	ret		nz
	
	;reload counter
	push	af
	ld		a,SHOP_FIRE_PERIOD
	ld		[hl],a
	ld		b,0
	ld		c,6
	add		hl,bc							;hl points at index
	ld		a,[hl]
	call	next_fire_scene
	;a holds next scene 1st sprite
	ld		[hl],a							;save next index scene
	pop		bc								;b holds index position
	push	af								;a holds index scene
	
	
	ld		hl,shop_fire_struct+14			;first scene byte
	;hl + (index*8)
[3]	sla		b
	ld		c,b
	ld		b,0
	add		hl,bc							

	pop		af
	push	hl
	ld		hl,fire_scene
	ld		b,0
	ld		c,a
	add		hl,bc
	ld		a,[hl]
	
	pop		hl
	ld		[hl],a
[4]	inc		hl
[4]	inc		a								;second sprite , scene + 4
	ld		[hl],a

	ret




display_shop_fires_fx:
	ld		hl,shop_fire_struct+12
	ld		de,SPRATR
	ld		bc,4*2*6
	call	0x5c

	ret





display_debris_fx:

	ld		hl,debris_fx_spr_struct
	ld		de,SPRATR+(4*19)
	ld		bc,40
	call	0x5c


	ret



ACTIVE_WIZARD_PERIOD	equ		5

;active wizard name will be flashing
;the other will stand still
;variables used 
;	shop_flashing_flags_counter
;	turn		(0=player,1=cpu)
;	shop_action holds current enemy name
;	quest_current_enemy  0-first evil mage, 1-second ....
;	quest_battle         0-training , 		1-quest battle
;	
update_and_display_wizards_names:
	
	ld		hl,shop_flashing_flags_counter
	dec		[hl]
	ret		nz

	ld		a,ACTIVE_WIZARD_PERIOD						;max period counter
	ld		[hl],a						;refresh counter
	
	ld		a,[turn]
	and		a
	jr		nz,update_and_display_wizards_names_cpu_flashing
	
	ld		a,[shop_action+6]
	and		a
	jr		z,update_and_display_wizards_names_player_show
	;hide player name
	
	xor		a
	ld		[shop_action+6],a
	
	ld		hl,NAMTBL
	ld		bc,6
	call	0x56
	
	jr		update_and_display_wizards_names_player_after
	
	
update_and_display_wizards_names_player_show:	
	ld		a,1
	ld		[shop_action+6],a
	
	call	display_player_name
	
update_and_display_wizards_names_player_after:	
	;show cpu name always
	call	display_cpu_name
	
	ret
	
	
	
update_and_display_wizards_names_cpu_flashing:
	ld		a,[shop_action+6]
	and		a
	jr		z,update_and_display_wizards_names_cpu_show
	;hide cpu
	xor		a
	ld		[shop_action+6],a
	
	ld		hl,NAMTBL+26
	ld		bc,6
	call	0x56
	
	jr		update_and_display_wizards_names_cpu_after
	
update_and_display_wizards_names_cpu_show:
	ld		a,1
	ld		[shop_action+6],a

	call	display_cpu_name

update_and_display_wizards_names_cpu_after:
	;show player always
	call	display_player_name
	
	ret




display_player_name:
	ld		hl,generic_player_name
	ld		de,NAMTBL+0
	ld		bc,6
	call	0x5c

	ret


display_cpu_name:
	ld		hl,shop_action
	ld		de,NAMTBL+26
	ld		bc,6
	call	0x5c
	
	ret


;updates shop_action with its name (6 chars max)
;TODO: shop_action is not a proper name ..... Check if shop_action is used for another purpose (I don't think so ... old stuff)
;	quest_current_enemy  0-first evil mage, 1-second ....
;	quest_battle         0-training , 		1-quest battle
; those variables must be updated
;returns shop_action updated 
set_enemy_name_for_later_flashing_use:
	;cleaning buffer
	xor		a
	ld		hl,shop_action
	ld		[hl],a
	ld		d,h
	ld		e,l
	inc		de
	ld		bc,5
	ldir
	;copy enemy name
	;quest?
	ld		a,[quest_battle]
	and		a
	jr		z,set_enemy_name_for_later_flashing_use_training
	ld		a,[quest_current_enemy]
	ld		hl,quest_enemy_list_ptr
	
	add		a,a
	add		a,a								;a=a*4=quest_current_enemy*4
	
	ld		b,0
	ld		c,a
	add		hl,bc	

	ld		e,[hl]
	inc		hl
	ld		d,[hl]
	ex		de,hl							;hl holds pointer to enemy quest name
	ld		a,[hl]							;first postion is length
	inc		hl
	jr		set_enemy_name_for_later_flashing_use_quest_copy_string

set_enemy_name_for_later_flashing_use_training:
	ld		a,6
	ld		hl,generic_enemy_name
set_enemy_name_for_later_flashing_use_quest_copy_string:

	ld		de,shop_action
	ld		b,0
	ld		c,a								;length
	ldir

	ret





flags_fx_init:
	ld		h,FLAG_SCENE_LIST_SIZE
	ld		l,3
	call	rand_max
		
	ld		ix,flags_fx_struct
	;scene index flag1,scene index flag2,counterflag1,counterflag2,maxcounter,flag1spr1 4 bytes sprattrb,flag1 spr2 4 bytes sprattrb,flag2 spr1/2 4+4
	;		0					1				2			3			4		5 6 7 8 					9 10 11 12					13 14 15 16     17 18 19 20
	ld		[ix],a
	xor		a
	ld		[ix+1],a
	
	;from index, set scene
	ld		a,[ix]
	ld		hl,flag_scene
	ld		b,0
	ld		c,a
	add		hl,bc
	ld		a,[hl]
	ld		[ix+7],a
[4]	inc		a
	ld		[ix+11],a
	
	ld		hl,flag_scene	
	ld		a,[hl]
	ld		[ix+15],a
[4]	inc		a
	ld		[ix+19],a
	
	
	;max counter
	ld		a,10											;Max flag period
	ld		[ix+4],a
	ld		[ix+2],a
	dec		a
	dec		a
	ld		[ix+3],a

	;x coord , it'sfixed!
	ld		a,0
	ld		[ix+6],a
	ld		[ix+10],a
	ld		a,240
	ld		[ix+14],a
	ld		[ix+18],a

	;y coord
	call	calc_flags_location_on_screen
	ld		[ix+5],h
	ld		[ix+9],h
	ld		[ix+13],l
	ld		[ix+17],l

	;color
	ld		a,9
	ld		[ix+8],a
	ld		a,10
	ld		[ix+16],a
	ld		a,15
	ld		[ix+12],a
	ld		a,14
	ld		[ix+20],a
	
	call	set_update_towerflag_flag
	
	ret





;TODO: I am sure I can refactor this and make it more compact
;[player_tower_height] and [cpu_tower_height] are taken to calculate y sprites position
;returns 	hl   h=Y left flag		l=Y right flag
calc_flags_location_on_screen:
	;player tower
	ld		a,[player_tower_height]
	and		a
	jr		z,calc_flags_location_on_screen_left_over

	ld		hl,tmp_buffer2
	call	binbcd

	ld		a,[tmp_buffer2]	
	ld		b,a
	ld		a,9*8						;base (when 0)	

[3]	sla		b	
	sub		b							;a=a-b
	ld		h,a							;save Y left coord on h

calc_flags_location_on_screen_after_left:
	;cpu_tower
	ld		a,[cpu_tower_height]
	and		a
	jr		z,calc_flags_location_on_screen_right_over		

	push	hl
	ld		hl,tmp_buffer2
	call	binbcd
	;call	calc_tower_wall
	ld		a,[tmp_buffer2]
	pop		hl
	ld		b,a
	ld		a,9*8						;base (when 0)	
	
[3]	sla		b
	sub		b							;a=a-b
	ld		l,a							;save Y right coord on h
	ret

calc_flags_location_on_screen_left_over:
	ld		h,193
	jr		calc_flags_location_on_screen_after_left
	
calc_flags_location_on_screen_right_over:
	ld		l,193
	ret







;a current index
;ret
;a next index
next_flag_scene:
	cp		FLAG_SCENE_LIST_SIZE
	jr		z,next_flag_scene_reset
	inc		a
	ret
next_flag_scene_reset:
	xor		a
	ret







update_towerflags_fx:
	ld		ix,flags_fx_struct
	;scene index flag1,scene index flag2,counterflag1,counterflag2,maxcounter,flag1spr1 4 bytes sprattrb,flag1 spr2 4 bytes sprattrb,flag2 spr1/2 4+4
	;		0					1				2			3			4		5 6 7 8 					9 10 11 12					13 14 15 16     17 18 19 20
	dec		[ix+2]
	jr		nz,update_towerflags_fx_update_flag2
	;update flag1
	;reset counter
	ld		a,[ix+4]
	ld		[ix+2],a
	;next scene
	ld		a,[ix]
	call	next_flag_scene
	ld		[ix],a
;TODO: should be called as routine?	 1
	ld		hl,flag_scene
	ld		b,0
	ld		c,a
	add		hl,bc
	ld		a,[hl]
;TODO: end
	ld		[ix+7],a
[4]	inc		a
	ld		[ix+11],a
	
	call	set_update_towerflag_flag

update_towerflags_fx_update_flag2:
	ld		a,[JIFFY]
	and		a
	ret		z											;sometimes lose counting in order to make it unsynchronized 
	dec		[ix+3]
	ret		nz
	;update flag2
	;reset counter
	ld		a,[ix+4]
	ld		[ix+3],a
	;next scene	
	ld		a,[ix+1]
	call	next_flag_scene
	ld		[ix+1],a
;TODO: should be called as routine?	 2
	ld		hl,flag_scene
	ld		b,0
	ld		c,a
	add		hl,bc
	ld		a,[hl]
;TODO: end
	ld		[ix+15],a
[4]	inc		a
	ld		[ix+19],a

	call	set_update_towerflag_flag

	ret






set_update_towerflag_flag:
	ld		a,[dump_flags]
	or		00010000b					;on update
	ld		[dump_flags],a
	ret






update_Y_flags_coord:
	call	calc_flags_location_on_screen
	;returns 	hl   h=Y left flag		l=Y right flag
	ld		a,h
	ld		[flags_fx_struct+5],a
	ld		[flags_fx_struct+9],a
	ld		a,l
	ld		[flags_fx_struct+13],a
	ld		[flags_fx_struct+17],a
	
	call	set_update_towerflag_flag
	
	ret






;TODO: think if we can avoid to do it for every single  frame
display_towerflags_fx:
	;always
	;ld		a,[dump_flags]
	;bit		4,a
	;ret		z
	
	ld		hl,flags_fx_struct+5
	ld		bc,4*4
	ld		de,SPRATR + (13*4)			;13,14,15,16
	call	0x05c
	
	;always
	;ld		a,[dump_flags]
	;and		11101111b					;off update
	;ld		[dump_flags],a	

	ret






;a= BFX0 || BFX1
;BFX0 perseids
;BFX1 thunder ;TODO: not implemented yet!!!!
background_fx_init:
	ld		[background_fx_struct],a					;save fx number
	cp		BFX0
	jr		z,background_fx_init_BFX0
	;BFX1

	;hidden firstly
	ld		ix,background_fx_struct
	ld		a,193
	ld		[ix+4],a					;y spr1
	ld		[ix+8],a					;y spr2

	ret

background_fx_init_BFX0
	xor		a
	ld		ix,background_fx_struct
	ld		[ix+1],a					;state 0
	ld		a,5										;ticks per scene
	ld		[ix+2],a					;counter
	ld		[ix+3],a					;max counter

	ret







update_background_fx:
	call	begin_background_fx

	ld		ix,background_fx_struct
	dec		[ix+2]										;dec counter
	ret		nz
	
	;found out what fx it is (switch)
	ld		a,[ix]
	;change state
	cp		BFX0
	jr		z,update_background_fx_BFX0
	;BFX1
	ret
update_background_fx_BFX0:								;another switch
	;4 states, 1,2,3,4
	ld		a,[background_fx_struct+1]
	cp		1
	jr		z,update_background_fx_BFX0_st1
	cp		2
	jr		z,update_background_fx_BFX0_st2
	cp		3
	jr		z,update_background_fx_BFX0_st3
	cp		4
	jr		z,update_background_fx_BFX0_st4
	;5 clean
	ld		a,193										;hidden
	ld		[ix+4],a					;spr1 Y 
	ld		[ix+8],a					;spr2 Y	
	
	xor		a
	jr		update_background_fx_end
update_background_fx_BFX0_st1:
	;location, scene (sprite pattern), 
	;random location
	call	get_random_background_position				;hl,  xy	
	ld		a,l
	ld		[ix+4],a					;spr1 Y 
	ld		a,193	;hidden
	ld		[ix+8],a					;spr2 Y	
	ld		a,h
	ld		[ix+5],a					;spr1 X 
	ld		[ix+9],a					;spr2 X
	ld		a,28										;spr1(first scene 7*4)
	ld		[ix+6],a
		

	ld		a,15
	ld		[ix+7],a					;color spr1
	ld		a,5
	ld		[ix+11],a					;color spr2
		
		
	ld		a,2
	jr		update_background_fx_end
update_background_fx_BFX0_st2:
	;scene, sprt2 y
	ld		a,32										;spr1 scene
	ld		[ix+6],a
	ld		a,36										;spr2 scene
	ld		[ix+10],a

	ld		a,[ix+4]
	ld		[ix+8],a					;spr2 Y	




	ld		a,5
	ld		[ix+7],a					;color spr1
	ld		a,15
	ld		[ix+11],a					;color spr2



	ld		a,3
	jr		update_background_fx_end
update_background_fx_BFX0_st3:
	;scene
	ld		a,40										;spr1 scene
	ld		[ix+6],a
	ld		a,44										;spr2 scene
	ld		[ix+10],a

/*
	ld		a,5
	ld		[ix+7],a					;color spr1
	ld		a,15
	ld		[ix+11],a					;color spr2
*/


	ld		a,4	
	jr		update_background_fx_end
	
update_background_fx_BFX0_st4:	
	;scene and location
	ld		a,48										;spr2 scene
	ld		[ix+10],a
	ld		a,193
	ld		[ix+4],a					;spr1 Y hidden 


	ld		a,15
	ld		[ix+7],a					;color spr1
	ld		a,5
	ld		[ix+11],a					;color spr2



	ld		a,5
	
update_background_fx_end:
	ld		[ix+1],a					;set next state
	;update counter
	ld		a,[ix+3]	
	ld		[ix+2],a					;reset counter to max count

	ret





;updates background_fx_struct+1 if fx begins 
begin_background_fx:
	ld		a,[background_fx_struct+1]
	and		a
	ret		nz

	ld		a,[JIFFY]
	cp		2
	ret		nc
	; ... let's begin!
	ld		a,1
	ld		[background_fx_struct+1],a
	
	ret




display_background_fx:
	ld		a,[background_fx_struct]
	and		a
	ret		z
	
	ld		hl,background_fx_struct+4
	ld		de,SPRATR + (4*7)
	ld		bc,8
	call	0x05c

	ret



;z flag , 0=check_right, 1=check left
;l , current y coord
;ret l, fixed
fix_Y_perseids:
	jr		z,fix_Y_perseids_right
	;left
	ld		a,[player_tower_height]
	cp		70									;90 or more=> fix!
	jr		nc,fix_Y_perseids_fix
	ld		a,[player_wall_height]
	cp		70									
	jr		nc,fix_Y_perseids_fix
	ret

fix_Y_perseids_right:
	ld		a,[cpu_tower_height]
	cp		70									;90 or more=> fix!
	jr		nc,fix_Y_perseids_fix
	ld		a,[cpu_wall_height]
	cp		70									
	jr		nc,fix_Y_perseids_fix
	ret

fix_Y_perseids_fix:
	ld		a,l
	and		00001111b							;no more than 16!
	ld		l,a	
	ret




;returns hl (h=X, l=Y) , range x(0,40)(+26*8) y(0,31)
get_random_background_position:
	;x
	ld		h,40
	ld		l,63
	call	rand_max
	push	af

	;y
	ld		h,31
	ld		l,31
	call	rand_max
	
	add		8				;avoid y top
	
	ld		l,a
	pop		af
	bit		0,a
	
	push	af
	call	fix_Y_perseids
	pop		af
		
	jr		nz,get_random_background_position_left
	add		208								;display it on the right

get_random_background_position_left:
	ld		h,a

	ret






load_tinymage_tiles:
	ld		hl,tinymagebank2_tiles
	ld		de,CHRTBL+(256*8)+(249*8)
	call	DEPLET

	ld		hl,tinymagebank2_colors
	ld		de,CLRTBL+(256*8)+(249*8)
	call	DEPLET
	
	ld		hl,tinymagebank3_tiles
	ld		de,CHRTBL+(256*8*2)+(249*8)
	call	DEPLET

	ld		hl,tinymagebank3_colors
	ld		de,CLRTBL+(256*8*2)+(249*8)
	call	DEPLET

	ret
	

;hl	- screen tiles to display (21x8)
display_battle_finished_message:
	ld		de,graphics_buffer
	call	unpack
	
	ld		b,8
	ld		de,NAMTBL + (8*32)+5
	ld		hl,graphics_buffer
display_battle_finished_message_loop:	
	push	bc
	push	hl
	push	de
	
	ld		bc,21
	call	0x005c
	
	pop		de
	pop		hl
	
	ex		de,hl
	ld		bc,32
	add		hl,bc
	ex		de,hl
	ld		bc,21
	add		hl,bc
	
	pop		bc
	djnz	display_battle_finished_message_loop


	ret


;hl	- screen tiles to display (21x8) 
display_tinymage_and_message:
	push	hl
	call	load_tinymage_tiles
	pop		hl
	call	display_battle_finished_message
	
	ld		hl,tiny_mage_scr
	ld		de,NAMTBL+(14*32)+23
	ld		bc,4
	call	0x05c
	
	ld		hl,tiny_mage_scr+4
	ld		de,NAMTBL+(15*32)+23
	ld		bc,4
	call	0x05c

	ld		hl,tiny_mage_scr+8
	ld		de,NAMTBL+(16*32)+23
	ld		bc,4
	call	0x05c

	ret


;hl pointer to name text
;de	vram location
display_enemyname:
	ld		b,0
	ld		a,[hl]
	ld		c,a							;bc string legth
	inc		hl							;hl first character
	call	0x005c

	ret
	
	

	

display_quit_cursor:
	;first clean previous curson position
	ld		a,[graphics_buffer+(9*7*2)+1]
	and		a
	jr		nz,display_quit_cursor_clean_no
	;clean yes
	ld		hl,NAMTBL+ (11*32)+14+32
	jr		display_quit_cursor_clean
display_quit_cursor_clean_no:	
	ld		hl,NAMTBL+ (11*32)+14
display_quit_cursor_clean:	
	xor		a
	ld		bc,2
	call	0x0056


	ld		a,[graphics_buffer+(9*7*2)+1]
	and 	a
	jr		z,display_quit_cursor_at_yes
	;no
	ld		de,NAMTBL+ (11*32)+14+32
	jr		display_quit_cursor_draw
display_quit_cursor_at_yes	
	ld		de,NAMTBL+ (11*32)+14
display_quit_cursor_draw:
	ld		hl,main_menu_cursor
	ld		bc,2
	call	0x005c
	
	ret
	
	
	
	
	
display_quit_dialog:
	;TODO: routine1, 2 and routine3 are similar
	;routine 1
	;first save background in graphics_buffer
	;9x7 bytes
	ld		b,7
	
	ld		hl,NAMTBL+QUIT_DIALOG_LOCATION
	ld		de,graphics_buffer
display_quit_dialog_save_loop:	
	push	bc
	push	hl
	push	de
		
	ld		bc,9
	call	0x0059
	
	ld		bc,9
	pop		hl		;de
	add		hl,bc
	ex		de,hl
	
	ld		bc,32
	pop		hl		;hl
	add		hl,bc
	pop		bc	
	djnz	display_quit_dialog_save_loop


	;load onto graphics_buffer+(9*7) unpacked quit_screen
	ld		hl,quit_screen
	ld		de,graphics_buffer+(9*7)
	call	unpack


	;TODO: routine1, 2 and routine3 are similar
	;routine 2
	;display on screen
	ld		b,7
	ld		hl,graphics_buffer+(9*7)
	ld		de,NAMTBL+QUIT_DIALOG_LOCATION
display_quit_dialog_actual_loop:	
	push	bc
	push	hl
	push	de
		
	ld		bc,9
	call	0x005c
	
	halt	
	halt	
	halt	;beautiful delay

	ld		bc,32
	pop		hl		;de
	add		hl,bc
	ex		de,hl
	
	ld		bc,9
	pop		hl		;hl
	add		hl,bc
	pop		bc	
	djnz	display_quit_dialog_actual_loop

	ret


;graphics_buffer must hold saved 9x7 tiles background 
display_restore_quit:
	;TODO: routine1, 2 and routine3 are similar
	;routine 3

	ld		b,7
	ld		hl,graphics_buffer
	ld		de,NAMTBL+QUIT_DIALOG_LOCATION
display_restore_quit_loop:	
	push	bc
	push	hl
	push	de
		
	ld		bc,9
	call	0x005c
	
	halt	
	halt	
	halt	;beautiful delay

	ld		bc,32
	pop		hl		;de
	add		hl,bc
	ex		de,hl
	
	ld		bc,9
	pop		hl		;hl
	add		hl,bc
	pop		bc	
	djnz	display_restore_quit_loop


	ret




remove_loadsave_status:
	;remove status line when key is pressed
	xor		a
	ld		hl,NAMTBL + MMENU_FIRST_OPTION_START + (32*7) 
	ld		bc,10
	call	0x0056
	ret


load_logo:
	;patterns
	ld		hl,logo_patterns
	ld		de,CHRTBL+(1*8)
	ld		bc,87*8
	call	DEPLET

	;colors
	ld		hl,logo_patterns_colors
	ld		de,CLRTBL+(1*8)
	ld		bc,87*8
	call	DEPLET

	ret




display_logo:
	ld		hl,logo_scr
	ld		de,graphics_buffer
	call	unpack
	
	ld		b,4
	ld		hl,graphics_buffer		
	ld		de,NAMTBL+1
	
display_logo_loop:
	push	bc	
	push	hl
	push	de
		
	ld		bc,28
	call	0x5c

	pop		hl		;de
	ld		de,32
	add		hl,de
	ex		de,hl
	
	pop		hl
	ld		bc,28
	add		hl,bc
		
	pop		bc
	djnz	display_logo_loop

	ld		a,80
	ld		hl,NAMTBL+32+29
	call	0x04d

	ld		a,85
	ld		hl,NAMTBL+32+29+32
	call	0x04d


	ret




	;card_detail_buffer LCARDART_F_TILE or RCARDART_F_TILE
	;card_detail_buffer+1 color flag
	;card_detail_buffer+2 card drawing begins 
battle_update_card_font_color:

	ld		hl,card_detail_buffer+1
	ld		a,[hl]
	and		00000111b										;just color flags
	
	bit		0,a
	jr		nz,battle_update_card_font_color_brown
	bit		1,a
	jr		nz,battle_update_card_font_color_blue
	;green
	ld		a,0x1C
	jr		battle_update_card_font_color_brownbluegreen_end
battle_update_card_font_color_brown:	
	ld		a,0x16
	jr		battle_update_card_font_color_brownbluegreen_end
battle_update_card_font_color_blue:
	ld		a,0x14
battle_update_card_font_color_brownbluegreen_end:

	push	af												;save color

	ld		hl,card_detail_buffer
	ld		a,[hl]
	cp		LCARDART_F_TILE
	jr		z,battle_update_card_font_color_leftcard			;zero = left card
	;right card
	pop		af
	ld		hl,RCARDART_VRAM_EDGE_B2_COLOR
	push	hl
	ld		hl,RCARDART_VRAM_EDGE_B1_COLOR
	push	hl

	ld		hl,RCARDART_VRAM_FONT_B2_COLOR + (21*8)
	push	hl
	ld		hl,RCARDART_VRAM_FONT_B2_COLOR + (17*8)
	push	hl
	ld		hl,RCARDART_VRAM_FONT_B2_COLOR + (5*8)
	push	hl
	ld		hl,RCARDART_VRAM_FONT_B2_COLOR
	push	hl

	ld		hl,RCARDART_VRAM_FONT_B1_COLOR + (21*8)
	push	hl
	ld		hl,RCARDART_VRAM_FONT_B1_COLOR + (17*8)
	push	hl
	ld		hl,RCARDART_VRAM_FONT_B1_COLOR + (5*8)
	push	hl
	ld		hl,RCARDART_VRAM_FONT_B1_COLOR
	push	hl


	jr		battle_update_card_font_color_leftorrightcardalreadyset
battle_update_card_font_color_leftcard:
	;left card
	pop		af
	ld		hl,LCARDART_VRAM_EDGE_B2_COLOR
	push	hl
	ld		hl,LCARDART_VRAM_EDGE_B1_COLOR
	push	hl

	ld		hl,LCARDART_VRAM_FONT_B2_COLOR + (21*8)
	push	hl
	ld		hl,LCARDART_VRAM_FONT_B2_COLOR + (17*8)
	push	hl
	ld		hl,LCARDART_VRAM_FONT_B2_COLOR + (5*8)
	push	hl
	ld		hl,LCARDART_VRAM_FONT_B2_COLOR
	push	hl

	ld		hl,LCARDART_VRAM_FONT_B1_COLOR + (21*8)
	push	hl
	ld		hl,LCARDART_VRAM_FONT_B1_COLOR + (17*8)
	push	hl
	ld		hl,LCARDART_VRAM_FONT_B1_COLOR + (5*8)
	push	hl
	ld		hl,LCARDART_VRAM_FONT_B1_COLOR
	push	hl


battle_update_card_font_color_leftorrightcardalreadyset:
	;font bank1
	pop		hl													;retrieves color
	push	af													;save a color
	;a set  , color
	;hl set , vram dest
	ld		bc,4*8
	call	0x56
	
	pop		af				
	pop		hl				
	push	af				
	ld		bc,10*8
	call	0x56

	pop		af				
	pop		hl				
	push	af				
	ld		bc,3*8
	call	0x56

	pop		af				
	pop		hl				
	push	af				
	ld		bc,27*8
	call	0x56

	
	
	;font bank2
	pop		af
	pop		hl													;retrieves color
	push	af													;save a color
	;a set  , color
	;hl set , vram dest
	ld		bc,4*8
	call	0x56
	
	pop		af				
	pop		hl				
	push	af				
	ld		bc,10*8
	call	0x56

	pop		af				
	pop		hl				
	push	af				
	ld		bc,3*8
	call	0x56

	pop		af				
	pop		hl				
	push	af				
	ld		bc,27*8
	call	0x56


	pop		af
	;;;;;;;;;;;;;;;;;;;;;;; TODO: Talk with SPOTA about this. Edges have reverse color
	ld		b,a
	sla		a
	sla		a
	sla		a
	sla		a
	
	sra		b
	sra		b
	sra		b
	sra		b
	
	or		b
	;;;;;;;;;;;;;;;;;;;;;;
	
	pop		hl
	push	af
	;edge bank1
	ld		bc,10*8			;10 tiles
	call	0x56

	pop		af
	pop		hl
	;edge bank2
	ld		bc,8*8			;8 tiles
	call	0x56


	ret





display_title_screen:

	ld		hl,display_title_screen_loop
	push	hl								;ret value

	call	activate_rom_page3

	ld		bc,ret_address_1
	ld		hl,title_screen_tiles
	ld		de,CHRTBL
	jp		unpack_rle_2_vram

ret_address_1:

	ld		bc,ret_address_2
	ld		hl,title_screen_colors
	ld		de,CLRTBL
	jp		unpack_rle_2_vram

ret_address_2:
	jp		restore_ram_page3

	
display_title_screen_loop:	
	halt	
	
	ld		hl,KEY_SPACE
	call	get_keyboard
	and		a
	ret		nz
	jr		display_title_screen_loop






update_cursor_flashing:

	ld		hl,cursor_flash_struct+1
	dec		[hl]
	ret		nz
	
	ld		a,5
	ld		[cursor_flash_struct+1],a
	ld		a,[cursor_flash_struct]
	and		a
	jr		z,update_cursor_flashing_state1
	;state2
	ld		a,4
	ld		[cursor_sprites_attr+(4*1)+3],a
	ld		a,10
	ld		[cursor_sprites_attr+(4*4)+3],a

	xor		a
	jr		update_cursor_flashing_endstate
update_cursor_flashing_state1:	
	;state1
	ld		a,8
	ld		[cursor_sprites_attr+(4*1)+3],a
	ld		a,4
	ld		[cursor_sprites_attr+(4*4)+3],a

	ld		a,1
update_cursor_flashing_endstate:
	ld		[cursor_flash_struct],a

	ret





update_tinymage_eyes:
	call	next_tinymage_eyes_color
	;a already set
	ld		hl,SPRATR + (6*4)+3			;color location for tinymage eyes color
	call	0x004D						;write one byte on VRAM

	ret
	

cls_tinymage_eyes:
	ld		a,193
	ld		hl,SPRATR + (6*4)			;Y location for tinymage eyes 
	call	0x004D						;write one byte on VRAM

	ret	



;a = number of coins earned
display_player_battle_quest_wins:
	push	af
	call	cls_screen
	;call	hide_cursor_sprites

	ld		hl,battle_finished_boss_victory
	call	display_tinymage_and_message
	
	pop		af
	ld		hl,tmp_buffer3
	
	call	binbcd_3digits
	ld		hl,tmp_buffer3
	ld		a,ASC_0
	add		a,[hl]
	ld		[hl],a
	inc		hl
	ld		a,ASC_0
	add		a,[hl]
	ld		[hl],a
	inc		hl
	ld		a,ASC_0
	add		a,[hl]
	ld		[hl],a
	
	;call	calc_two_digits_counter_dest_hl
	;tmp_buffer3 and tmp_buffer3+1 contains number of coins
	ld		hl,tmp_buffer3
	ld		de,NAMTBL+(12*32)+22
	ld		bc,3
	call	0x005c	
	halt

	ret

;a contains number of coins earned
display_player_battle_wins:
	push	af
	call	cls_screen
	;call	hide_cursor_sprites
	
	ld		hl,battle_finished_training_win
	call	display_tinymage_and_message
	
	pop		af
	
	ld		hl,tmp_buffer3
	call	calc_two_digits_counter_dest_hl
	;tmp_buffer3 and tmp_buffer3+1 contains number of coins
	ld		hl,tmp_buffer3
	ld		de,NAMTBL+(11*32)+23
	ld		bc,2
	call	0x005c
		
	halt

	ret
	


display_player_battle_lost:
	call	cls_screen
	;call	hide_cursor_sprites


	ld		hl,battle_finished_training_lose
	call	display_tinymage_and_message

	halt

	ret


;a = card being stolen
display_player_battle_quest_lost:
	push	af
	call	cls_screen
	;call	hide_cursor_sprites

	ld		hl,battle_finished_boss_lose
	call	display_tinymage_and_message

	pop		af

	call	get_card_address
	push	hl
	pop		iy
	ld		h,[iy+21]
	ld		l,[iy+20]					;hl=pointer to the title
	
	push	hl
	pop		ix							;ix title text address, src
	ld		hl,tmp_buffer3				;hl dest
	xor		a
	ld		[hl],a
	ld		d,h
	ld		e,l
	inc		de
	ld		bc,15
	ldir	
					
	ld		hl,tmp_buffer3				;hl dest											
	ld		a,2							;number of lines
	ld		b,1							;bytes to skip after every line
	ld		c,0							;font offset
	
	
	call	print_string0_mem_with_skip	;card name
	ld		hl,tmp_buffer3
	ld		de,BATTLE_PLAYER_LOSES
	ld		bc,8
	call	0x005c
	ld		hl,tmp_buffer3+8
	ld		de,BATTLE_PLAYER_LOSES+32
	ld		bc,8
	call	0x005c

	halt

	ret


display_player_battle_quest_gameover:
	call	cls_screen
	;call	hide_cursor_sprites

	ld		hl,battle_finished_boss_gameover
	call	display_tinymage_and_message
	call	wait_press_space_flashing_eyes
	;call	wait_press_space


	call	cls_screen
	ld		hl,battle_quest_gameover
	ld		de,BATTLE_PLAYER_GAMEOVER_TXT_LOCATION
	ld		bc,9
	call	0x005c

	halt

	ret


display_player_gameover_finalboss_defeated:
	call	cls_screen
	;call	hide_cursor_sprites

	;11,4
	ld		hl,battle_player_win_gameover1
	ld		de,NAMTBL+(4*32+5)
	ld		bc,25
	call	0x005c
	ld		hl,battle_player_win_gameover2
	ld		de,NAMTBL+(5*32+5)
	ld		bc,24
	call	0x005c
	ld		hl,battle_player_win_gameover3
	ld		de,NAMTBL+(6*32+11)
	ld		bc,11
	call	0x005c




	
	;13,9
	ld		hl,NAMTBL+(9*32+13)
	ld		[graphics_buffer+8],hl
	ld		a,96
	ld		b,6
.loop_outter:
	push	bc
	ld		hl,graphics_buffer
	ld		b,8
.loop_inner:
	push	bc
	ld		[hl],a
	inc		hl
	inc		a
	pop		bc
	djnz	.loop_inner
	
	ld		de,[graphics_buffer+8]
	ld		hl,graphics_buffer
	ld		bc,8
	push	af
	call	0x05c
	pop		af
	
	ld		de,[graphics_buffer+8]
	ld		hl,32
	add		hl,de
	ld		[graphics_buffer+8],hl

	pop		bc
	djnz	.loop_outter



	ld		hl,display_player_gameover_finalboss_defeated_end
	push	hl								;ret value

	call	activate_rom_page3

	ld		bc,display_player_gameover_finalboss_defeated_1
	ld		hl,finale_patterns
	ld		de,CHRTBL+(256*8)+(96*8)
	jp		unpack_rle_2_vram
display_player_gameover_finalboss_defeated_1:		

	ld		bc,display_player_gameover_finalboss_defeated_2
	ld		hl,finale_colors
	ld		de,CLRTBL+(256*8)+(96*8)
	jp		unpack_rle_2_vram
display_player_gameover_finalboss_defeated_2:

	jp		restore_ram_page3

display_player_gameover_finalboss_defeated_end:

	halt

	ret





;TODO: please sort this out
display_update_shop:

	ld		a,[dump_flags]
	bit		2,a
	jr		z,display_update_shop_after_filter

	;filter
	call	display_filter

display_update_shop_after_filter:
	ld		a,[dump_flags]
	bit		4,a
	jr		z,display_update_shop_after_counters

	;counters
	call	display_shop_counters
display_update_shop_after_counters:
	ld		a,[dump_flags]
	bit		1,a
	jr		z,display_update_shop_after_deck

	;display deck
	call	shop_display_deck
display_update_shop_after_deck:
	ld		a,[dump_flags]
	bit		3,a
	jr		z,display_update_shop_after_action

	;action
	call	display_shop_action
display_update_shop_after_action:	
	ld		a,[dump_flags]
	bit		0,a
	jr		z,display_update_shop_after_detail
	
	;card detail
	call	shop_display_card_detail
display_update_shop_after_detail:
		
		
	call	shop_display_flashing_tiles
		
		
	ret



shop_display_flashing_tiles:
	ld		a,[shop_flashing_flags_counter]
	and		01111111b
	dec		a			;is a==1?
	jr		z,shop_display_flashing_tiles_redraw
	;nothing to do, don't redraw
	ret
	
shop_display_flashing_tiles_redraw:
	ld		a,[shop_flashing_flags_counter]
	bit		7,a
	jr		z,shop_display_flashing_tiles_remove
	ld		hl,NAMTBL+(256)+32+32+10
	ld		a,58					;arrow top
	call	0x004D
	ld		hl,NAMTBL+(256)+32+32++32+10
	ld		a,59					;arrow bottom
	call	0x004D
	jr		shop_display_flashing_tiles_check_buy
shop_display_flashing_tiles_remove:
	ld		hl,NAMTBL+(256)+32+32+10
	ld		a,252					;arrow1 second scene
	call	0x004D
	ld		hl,NAMTBL+(256)+32+32++32+10
	ld		a,253					;arrow2 second scene
	call	0x004D

shop_display_flashing_tiles_check_buy:
	ld		a,[dump_flags]
	bit		5,a
	ret		z
	ld		a,[shop_flashing_flags_counter]
	bit		7,a
	jr		z,shop_display_flashing_tiles_remove_buy
	ld		hl,NAMTBL+(256)+32+32+32+11
	ld		a,64					;buy text1
	call	0x004D
	ld		hl,NAMTBL+(256)+32+32+32+11+1
	ld		a,65					;buy text2
	call	0x004D
	
	ret
	
shop_display_flashing_tiles_remove_buy:
	ld		hl,NAMTBL+(256)+32+32+32+11
	ld		a,38							;background
	call	0x004D
	ld		hl,NAMTBL+(256)+32+32+32+11+1
	ld		a,38							;background
	call	0x004D

	ret




;a = card number
print_shop_action:
	
	ld		hl,player_deck_status
	ld		d,0
	ld		e,a
	add		hl,de
	;dec		hl
	
	ld		a,[hl]
	bit		6,a
	jr		nz,print_shop_action_owned
	;not owned

	ld		a,[shop_card_list+3]
	call	get_card_address
	inc		hl
	inc		hl
	ld		a,[hl]					;card weight
	
	call	get_card_price
	
	ld		hl,shop_action
	call	calc_two_digits_counter_dest_hl
	
	ld		hl,shop_action+2
	ld		a,38					;background
	ld		[hl],a
	ld		de,shop_action+3
	ld		bc,5
	ldir
		
	;set flashing buy flag
	ld		a,[dump_flags]
	or		00100000b
	ld		[dump_flags],a	
	
	ret

print_shop_action_owned:
	bit		7,a
	jr		nz,print_shop_action_in
	;is out
	ld		hl,shop_action_in
	jr		print_shop_action_end
	
print_shop_action_in:				;already in
	ld		hl,shop_action_out

print_shop_action_end:
	ld		de,shop_action
	ld		bc,8
	ldir
	
	;remove flashing buy flag
	ld		a,[dump_flags]
	and		11011111b
	ld		[dump_flags],a		

	ret
	
	
;shop_action already set
display_shop_action:
	ld		hl,shop_action
	ld		de,NAMTBL + SHOP_ACTION
	ld		bc,4
	call	0x005c
	
	ld		hl,shop_action+4
	ld		de,NAMTBL + SHOP_ACTION + 32
	ld		bc,4
	call	0x005c
	
	;remove flag
	ld		a,[dump_flags]
	and		11110111b
	ld		[dump_flags],a	
	
	ret

	
	

;print shop counters on mem
print_shop_counters:
	
	ld		a,[shop_cards_in_deck]
	ld		hl,shop_counters_buffer
	call	calc_three_digits_counter

	ld		a,[shop_cards_owned]
	ld		hl,shop_counters_buffer+3
	call	calc_three_digits_counter


	ld		hl,[shop_coins]
	;ld		a,h
	;ld		h,l
	;ld		l,a
	ld		de,shop_counters_buffer+6
	call	Num2Dec	
	;ld		hl,shop_counters_buffer+6
	;call	calc_three_digits_counter


	ret



;display on screen
display_shop_counters:


	ld		hl,shop_counters_buffer
	ld		de,NAMTBL+SHOP_CARDSINDECK_COUNTER
	call	print_three_digit_counter
	
	ld		hl,shop_counters_buffer+3
	ld		de,NAMTBL+SHOP_CARDSOWNER_COUNTER
	call	print_three_digit_counter
	
	ld		hl,shop_counters_buffer+6
	ld		de,NAMTBL+SHOP_COINS_COUNTER
	call	print_three_digit_counter
	
		;remove flag
	ld		a,[dump_flags]
	and		11101111b
	ld		[dump_flags],a

	ret

	






;leaves filter drawing on filter_buffer ready to print depending on shop_filter var
print_filter:
	
	ld		de,filter_buffer	
	ld		a,[shop_filter]
	and		a
	jr		nz,print_filter_pos1_greyout
	ld		hl,shop_filter_all
	jr		print_filter_print_pos1
print_filter_pos1_greyout:
	ld		hl,shop_filter_all_greyout
print_filter_print_pos1:	
	call	print_filter_color
	
	
	ld		de,filter_buffer+5
	ld		a,[shop_filter]
	dec		a
	jr		nz,print_filter_pos2_greyout
	ld		hl,shop_filter_red
	jr		print_filter_print_pos2
print_filter_pos2_greyout:
	ld		hl,shop_filter_red_greyout
print_filter_print_pos2:	
	call	print_filter_color
	
	
	ld		de,filter_buffer+10
	ld		a,[shop_filter]
	cp		2
	jr		nz,print_filter_pos3_greyout
	ld		hl,shop_filter_blue
	jr		print_filter_print_pos3
print_filter_pos3_greyout:
	ld		hl,shop_filter_blue_greyout
print_filter_print_pos3:	
	call	print_filter_color
	

	ld		de,filter_buffer+15
	ld		a,[shop_filter]
	cp		3
	jr		nz,print_filter_pos4_greyout
	ld		hl,shop_filter_green
	jr		print_filter_print_pos4
print_filter_pos4_greyout:
	ld		hl,shop_filter_green_greyout
print_filter_print_pos4:	
	call	print_filter_color

	ret



display_filter:
	ld		b,4
	ld		de,NAMTBL+SHOP_FILTER_CARDS
	ld		hl,filter_buffer
display_filter_loop:	
	push	bc
	push	hl
	push	de
	
	
	ld		bc,0x0014
	call	0x005C
			
	pop		hl
	ld		bc,0x0020
	add		hl,bc
	ex		de,hl	
	pop		hl
	ld		bc,0x0014
	add		hl,bc
		
	pop		bc
	djnz	display_filter_loop

	;remove flag
	ld		a,[dump_flags]
	and		11111011b
	ld		[dump_flags],a

	ret



;hl source
;de dest
print_filter_color:
	ld		b,4
print_filter_color_loop:
	push	bc
	push	hl
	push	de
	
	ld		bc,0x0005
	ldir	

	pop		hl
	ld		b,0
	ld		c,20
	add		hl,bc
	ex		de,hl		;de set
	pop		hl
	ld		b,0
	ld		c,5
	add		hl,bc		;hl	set
	
	pop		bc
	djnz	print_filter_color_loop

	ret




shop_load_patterns:
/*
	ld		hl,shop_patterns
	ld		de,CHRTBL
	call	DEPLET
	*/

	ld		hl,shop_load_patterns_after_tiles
	push	hl								;ret value

	call	activate_rom_page3

	ld		bc,shop_load_patterns_ret_address_1
	ld		hl,shop_patterns
	ld		de,CHRTBL
	jp		unpack_rle_2_vram

shop_load_patterns_ret_address_1:

	jp		restore_ram_page3


shop_load_patterns_after_tiles:
;colors
	ld		hl,shop_patterns_colors
	ld		de,CLRTBL
	call	DEPLET


	ret




shop_display_init_screen:

	;load base blank screen
	call	shop_load_screen
	
	call	print_filter
	call	print_shop_counters
	call	shop_print_deck

	ld		a,00011111b
	ld		[dump_flags],a
	
	ld		a,[shop_card_list+3]
	push	af
	call	shop_print_card_details_mem
	pop		af
	call	print_shop_action
	
	
	ld		a,2
	ld		[shop_flashing_flags_counter],a
	
	call	display_update_shop

	ret



;a card number
;card_detail_buffer will hold the card ready to be displayed
shop_print_card_details_mem:
	
	call	get_card_address								;hl = card record address
	push	hl
	
	pop		iy
	ld		a,[iy+1]										;a = card flags
	
	bit		0,a
	jr		nz,shop_print_card_details_mem_red
	bit		1,a
	jr		nz,shop_print_card_details_mem_blue
	;green
	ld		hl,shop_green_card
	ld		c,SHOP_GREEN_FONT_OFFSET
	push	bc	
	jr	shop_print_card_details_mem_color_set
shop_print_card_details_mem_red:
	ld		hl,shop_red_card
	ld		c,SHOP_RED_FONT_OFFSET
	push	bc	
	jr	shop_print_card_details_mem_color_set
shop_print_card_details_mem_blue:	
	ld		hl,shop_blue_card
	ld		c,SHOP_BLUE_FONT_OFFSET
	push	bc	
	
shop_print_card_details_mem_color_set:	
	ld		de,card_detail_buffer
	
	push	iy
	call	unpack
	pop		iy
	
	
	;load card art on card_art_buffer+1	
	push	iy
	pop		ix
					;a is not needed
	push	iy
	call	load_card_art_ram
	pop		iy
	
	;write card title	
	ld		h,[iy+21]
	ld		l,[iy+20]					;hl=pointer to the title
	
	push	hl
	pop		ix							;ix title text address, src
	ld		hl,card_detail_buffer+11	;hl dest
										;c already set
	ld		a,2							;number of lines
	pop		bc
	push	bc							;save c for later use
	ld		b,2							;bytes to skip after every line

	call	print_string0_mem_with_skip	;title
	
	
	
	;write card text
	ld		h,[iy+23]
	ld		l,[iy+22]

	push	hl
	pop		ix							;ix src	
	ld		hl,card_detail_buffer+91	;hl dest
										;c already set
	ld		a,5							;number of lines
	pop		bc
	push	bc
	ld		b,2							;bytes to skip at the end of every single line

	call	print_string0_mem_with_skip	;print txt in buffer

	
	;write cost value
	ld		a,[iy+3]					;a=card cost
	ld		hl,graphics_buffer			;dest for two ascii digits
		
	call	calc_two_digits_counter_dest_hl			

	ld		hl,graphics_buffer+2
	xor		a
	ld		[hl],a						;set 0 to know when string is finished (if not it's gonna write a long line and not just two digits)
	;and send to buffer
	
	ld		hl,graphics_buffer
	push	hl
	pop		ix							;ix src	
	ld		hl,card_detail_buffer+141+6;hl dest
										;c already set
	ld		a,1							;number of lines
	pop		bc
	ld		b,2							;bytes to skip at the end of every single line					;TODO: probably not needed

	call	print_string0_mem_with_skip	;print txt in buffer
	

	ret





;card_detail_buffer, card_art_buffer+1 and card_art_buffer_color must be populated
shop_display_card_detail:
	ld		hl,card_detail_buffer
	ld		de,NAMTBL+(32*2)+15


	ld		a,16				;16 lines
shop_display_card_detail_card_loop:
	push	af
	ld		b,0
	ld		c,10
	push	hl					
	push	de					;save memsrc vramdest  

	call	0x005c
	ld		b,0
	ld		c,32

	pop		hl		;hl=de
	add		hl,bc	;de+(32)
	push	hl
	pop		de		;de=de+32

	ld		bc,10
	pop		hl
	add		hl,bc	;hl=hl+10

	pop		af
	dec		a
	jr		nz,shop_display_card_detail_card_loop
	
	halt										;sync
	;load card art on vram
	ld		hl,card_art_buffer+1
	ld		de,CHRTBL+8
	ld		bc,12*8
	call	0x005c

	ld		hl,card_art_buffer+1+(8*12)
	ld		de,CHRTBL+8 + (256*8)
	ld		bc,12*8
	call	0x005c
	
	;color
	ld		hl,card_art_buffer_color
	ld		de,CLRTBL+8
	ld		bc,12*8
	call	0x005c

	ld		hl,card_art_buffer_color+(8*12)
	ld		de,CLRTBL+8 + (256*8)
	ld		bc,12*8
	call	0x005c
	
		;remove flag
	ld		a,[dump_flags]
	and		11111110b
	ld		[dump_flags],a

	ret




;just one card
;hl	buffer dest
;a card number
shop_print_card_top:

	ld		[tmp_buffer3],a			;save card number for a while
	ld		[tmp_buffer3+1],hl		;same hl (dest)

	call	get_card_address
	push	hl
	pop		iy						;iy	card

	;color?
	ld		a,[iy+1]				;a=card flags
	bit		0,a						;red?
	jr		nz,shop_print_card_red
	bit		1,a						;blue?
	jr		nz,shop_print_card_blue
	;green!
	ld		hl,shop_topcards_template+60			;TODO: explain why +0 +30 +60 (card tops templates)
	ld		c,SHOP_GREEN_FONT_OFFSET
	push	bc
	jr		shop_print_card_color_set
shop_print_card_red:
	ld		hl,shop_topcards_template
	ld		c,SHOP_RED_FONT_OFFSET
	push	bc
	jr		shop_print_card_color_set
shop_print_card_blue:
	ld		hl,shop_topcards_template+30
	ld		c,SHOP_BLUE_FONT_OFFSET
	push	bc

shop_print_card_color_set:	
	ld		de,[tmp_buffer3+1]
	ld		bc,30
	ldir	

	
	
	;write card title	
	ld		h,[iy+21]
	ld		l,[iy+20]					;hl=pointer to the title
	
	push	hl
	pop		ix							;ix title text address, src
	
	ld		hl,[tmp_buffer3+1]
	ld		bc,11
	add		hl,bc						;hl = buffer dest + 1
				
	pop		bc							;c holds font offset
										;c already set
	ld		a,2							;number of lines
	ld		b,2							;bytes to skip after every line
	
	call	print_string0_mem_with_skip	;title
	

	;leave hl ready for next buffer address to write
	ld		hl,[tmp_buffer3+1]
	ld		de,30
	add		hl,de
	
	
	
	ret





;shop_card_list must be populated
;prints shop deck in shop_card_deck_buffer
shop_print_deck:

	ld		hl,shop_card_list
	push	hl
	pop		ix
	ld		a,[ix]				;ix=shopcard list

	push	ix
	ld		hl,shop_card_deck_buffer
	call	shop_print_card_top

	pop		ix
	push	ix
	;inc		hl
	ld		a,[ix+1]
	call	shop_print_card_top

	pop		ix
	push	ix
	;inc		hl
	ld		a,[ix+2]
	call	shop_print_card_top


	pop		ix
	push	ix
	;inc		hl
	ld		a,[ix+3]
	call	shop_print_card_top


	pop		ix
	push	ix
	;inc		hl
	ld		a,[ix+4]
	call	shop_print_card_top


	pop		ix
	;inc		hl
	ld		a,[ix+5]
	call	shop_print_card_top
	
	ret


	;render from shop_card_deck_buffer (display)
shop_display_deck:

	ld		b,18			;18 lines
	
	ld		hl,shop_card_deck_buffer	
	ld		de,NAMTBL
shop_display_deck_loop:
	push	bc
	push	hl
	push	de
		
	ld		bc,0x000a			;10 tiles
	call 	0x005c
	
	
	ld		b,0				;TODO: I am sure I can do this better!!!. Very old code
	ld		c,32
	pop		hl				;old de value
	add		hl,bc
	push	hl
	pop		de				;new dest
	
	pop		hl
	ld		b,0
	ld		c,10
	add		hl,bc			;src=src+12
	
	pop		bc
	djnz	shop_display_deck_loop
	
	;remove flag
	ld		a,[dump_flags]
	and		11111101b
	ld		[dump_flags],a

	ret



;updates cursor_sprites_attr buffer
setup_cursor_sprites:

	ld		ix,cursor_sprites_attr

	;colors
	ld		[ix+3],1		
	ld		[ix+7],8		
	ld		[ix+11],10		
	ld		[ix+15],1		
	ld		[ix+19],4		
	ld		[ix+23],8		

	;sprite patterns
	ld		[ix+2],0
	ld		[ix+6],4
	ld		[ix+10],8
	ld		[ix+14],12
	ld		[ix+18],16
	ld		[ix+22],20

	ret



;updates cursor sprites
display_cursor:
	ld		ix,cursor_sprites_attr

	ld		hl,cursor_data
	xor		a
	cp		[hl]			;sprites P or D selection
	ld		a,200
	jr		z,display_cursor_play_selected
	;D selected
	ld		[ix],a
	ld		[ix+4],a
	ld		[ix+8],a

	ld		[ix+12],CURSOR_Y		
	ld		[ix+16],CURSOR_Y
	ld		[ix+20],CURSOR_Y

	jr		display_cursor_color_end		
display_cursor_play_selected:
	;P selected
	ld		[ix],CURSOR_Y + 10
	ld		[ix+4],CURSOR_Y + 10
	ld		[ix+8],CURSOR_Y + 10

	ld		[ix+12],a	
	ld		[ix+16],a
	ld		[ix+20],a
	
display_cursor_color_end:	
							;sprites position selection	
	inc		hl				;hl pointing at mini card number (0-5)
	ld		b,24
	ld		c,[hl]
							;c=c*24
	push	bc
	pop		de				;de = bc 
	call	multiply		;hl = d * e
	push	hl
	pop		bc				;bc = c*24
							
							
	ld		hl,CURSOR_X_F
	add		hl,bc			;l = CURSOR_X final position sprites
	ld		[ix+1],l		
	ld		[ix+5],l		
	ld		[ix+9],l			

	ld		[ix+13],l		
	ld		[ix+17],l		
	ld		[ix+21],l		
	
	
	ld 		hl,cursor_sprites_attr
	ld 		de,SPRATR
	ld 		bc,6*4 ;(6 sprites * 4 attributes)
	call 	0x005c		;copia a memoria de video
	ret


	ret

;TODO:rename!!!
;hide cursor sprites and some fx	
hide_cursor_sprites:
	ld		ix,cursor_sprites_attr
	ld		a,193
	
	ld		[ix],a
	ld		[ix+4],a
	ld		[ix+8],a
	ld		[ix+12],a	
	ld		[ix+16],a
	ld		[ix+20],a	
	
	ld 		hl,cursor_sprites_attr
	ld 		de,SPRATR  ;0-5
	ld 		bc,6*4 ;(6 sprites * 4 attributes)
	call 	0x005c		;copia a memoria de video

	ld		ix,background_fx_struct+4
	ld		a,193
	ld		[ix],a
	ld		[ix+4],a
	ld 		hl,background_fx_struct+4
	ld 		de,SPRATR + (4*7)  ;7-8
	ld 		bc,2*4;
	call 	0x005c		;copia a memoria de video
	
	
	call	hide_player_flag
	
	ret
	
	
	
	
	
	
hide_player_flag:	
	;hide tower flags
	ld		a,193
	ld		[flags_fx_struct+5],a
	ld		[flags_fx_struct+5+4],a

	
	ld		hl,flags_fx_struct+5
	ld		bc,4*2
	ld		de,SPRATR + (13*4)
	call	0x05c	
	
	ret
	
	
	

;shows player minicards
display_player_hand:
	ld		ix,player_hand
	ld		a,[ix]
	
	push	ix
	ld		hl,player_tower_height
	call	is_card_playable	
	cpl		
	and		0x01
	ld		b,a
	pop		ix
	ld		a,[ix]
	ld		de,MINICARD_VRAM_0
	call	display_minicard


	ld		a,[ix+1]
	push	ix
	ld		hl,player_tower_height
	call	is_card_playable	
	cpl		
	ld		b,a
	pop		ix
	ld		a,[ix+1]
	ld		de,MINICARD_VRAM_1
	call	display_minicard


	ld		a,[ix+2]
	push	ix
	ld		hl,player_tower_height
	call	is_card_playable	
	cpl		
	ld		b,a
	pop		ix
	ld		a,[ix+2]
	ld		de,MINICARD_VRAM_2
	call	display_minicard


	ld		a,[ix+3]
	push	ix
	ld		hl,player_tower_height
	call	is_card_playable	
	cpl		
	ld		b,a
	pop		ix
	ld		a,[ix+3]	
	ld		de,MINICARD_VRAM_3
	call	display_minicard


	ld		a,[ix+4]
	push	ix
	ld		hl,player_tower_height
	call	is_card_playable	
	cpl		
	ld		b,a
	pop		ix
	ld		a,[ix+4]	
	ld		de,MINICARD_VRAM_4
	call	display_minicard


	ld		a,[ix+5]
	push	ix
	ld		hl,player_tower_height
	call	is_card_playable	
	cpl		
	ld		b,a
	pop		ix
	ld		a,[ix+5]	
	ld		de,MINICARD_VRAM_5
	call	display_minicard
	
	ret



;de vram dest address
;a card number
;b = 0 regular , 1 greyed out 
display_minicard:
	push	de				;(*1)
	
	ld		hl,tmp_buffer3
	ld		[hl],b
	push	hl
	pop		iy				;tmp_buffer contains flag to know whether greyed out or not 
	
	call	get_card_address

	;let's find out the card color
	inc		hl				;hl+1 is the flags byte
	ld		a,[hl]
	and		0x01			;TODO: bit comparison!!!
	cp		0x01			;brown?
	jr		z,display_minicard_brown
	ld		a,[hl]
	and		0x02
	cp		0x02			;blue?
	jr		z,display_minicard_blue
	;green
	bit		0,[iy]
	jr		nz,display_minicard_green_greyedout
	ld		hl,minicard_green
	jr		display_minicard_color_end
display_minicard_green_greyedout:
	ld		hl,minicard_green_greyedout
	jr		display_minicard_color_end

display_minicard_brown:	
	bit		0,[iy]
	jr		nz,display_minicard_brown_greyedout
	ld		hl,minicard_brown
	jr		display_minicard_color_end
display_minicard_brown_greyedout:
	ld		hl,minicard_brown_greyedout
	jr		display_minicard_color_end

	
display_minicard_blue:
	bit		0,[iy]
	jr		nz,display_minicard_blue_greyedout
	ld		hl,minicard_blue		
	jr		display_minicard_color_end	
display_minicard_blue_greyedout:	
	ld		hl,minicard_blue_greyedout
	
	
display_minicard_color_end:
	pop		de				;(*1)
	ld		bc,3
	ld		a,4				;a=number of rows	
display_minicard_loop:	
	push	hl				;
	push	de				;save hl,bc for later
	push	af
	call	0x05c			;draw row on vram
	pop		af
	dec		a
	jr		z,display_minicard_loop_end
	
	pop		hl
	ld		bc,32
	add		hl,bc		
	push	hl
	pop		de				;de ready	
	
	pop		hl
	ld		bc,3
	add		hl,bc			;hl	ready
							;bc already 3
	jr		display_minicard_loop
display_minicard_loop_end:
	pop		hl
	pop		de				;restore the stack

	ret







;a=LCARDART_F_TILE or RCARDART_F_TILE
;ix=pointing at the right card mem address
;doesn't modify VRAM, just gets ready for later use
;updating card_art_buffer and card_art_buffer_color buffers
;SAVES on card_art_buffer LCARDART_F_TILE or RCARDART_F_TILE  , card_art_buffer+1 starts actual graphics
load_card_art_ram:

	ld		hl,card_art_buffer
	ld		[hl],a							;saves whether right or left for render this buffer later on screen

	
;art graphics
	;unpack
	ld		h,[ix+25]		;			
	ld		l,[ix+24]		;
	inc		hl
	inc		hl				;hl=address where the card graphic begins
	
	push	ix
	
	di
	call	setrompage0
	
	ld		de,card_art_buffer_color
	call	unpack
	
	call	recbios
	ei

	pop		ix
;art colors

	;unpack
	ld		h,[ix+25]		;			
	ld		l,[ix+24]		;hl pointer to color table pointer
	push	hl
	pop		ix

	di
	call	setrompage0

	ld		h,[ix+1]		;			
	ld		l,[ix]		;hl where color table begins	

	ld		de,card_art_buffer+1
	
	call	unpack
	
	call	recbios
	ei

	ret




;Just moves to vram  graphics & color asap in order to display cart art
;so card_art_buffer & card_art_buffer_color MUST BE POPULATED
display_card_art:

	;left or right card?
	ld		hl,card_art_buffer
	ld		a,[hl]
	
	cp		LCARDART_F_TILE
	jr		z,display_card_updatevrampatternscolors_LCARDART_F_TILE
;colors
	ld		de,CPU_CARD_DETAIL_COL_BLK1	
	push	de
;patterns 
	ld		de,CPU_CARD_DETAIL_CH_BLK1	
	jr		display_card_updatevrampatternscolors_end

display_card_updatevrampatternscolors_LCARDART_F_TILE:	
;colors
	ld		de,PLAYER_CARD_DETAIL_COL_BLK1	
	push	de
;patterns
	ld		de,PLAYER_CARD_DETAIL_CH_BLK1	
display_card_updatevrampatternscolors_end:

	;character data dump
	;		de already holds vram dest ch bank1
	ld		hl,card_art_buffer+1				;first byte is just right or left card flag
	ld		bc,24*8
	call	0x005c

	pop		de
	;color data dump
	;		de already holds vram dest color bank1
	ld		hl,card_art_buffer_color
	ld		bc,24*8
	call	0x005c

	ret







card_dump:
	ld		hl,card_detail_buffer
	ld		a,[hl]
	cp		LCARDART_F_TILE	;left?	
	jr		z,card_dump_left_side_cardmem
	ld		de,CPU_CARD_DETAIL
	jr		card_dump_vram_dump	
card_dump_left_side_cardmem:	
	ld		de,PLAYER_CARD_DETAIL
card_dump_vram_dump:	
	;ld		hl,tmp_buffer
	ld		hl,card_detail_buffer+2

	ld		a,16				;16 lines
card_dump_vram_dump_loop:
	push	af
	ld		b,0
	ld		c,10
	push	hl					
	push	de					;save memsrc vramdest  

	call	0x005c
	ld		b,0
	ld		c,32

	pop		hl		;hl=de
	add		hl,bc	;de+(32)
	push	hl
	pop		de		;de=de+32

	ld		bc,10
	pop		hl
	add		hl,bc	;hl=hl+10

	pop		af
	dec		a
	jr		nz,card_dump_vram_dump_loop

	;clear flags left & right
	ld		a,[dump_flags]
	and		11111100b
	ld		[dump_flags],a


	ret






display_card:
	;change vram font & card color
	;card_detail_buffer has all info

	;card_detail_buffer LCARDART_F_TILE or RCARDART_F_TILE
	;card_detail_buffer+1 color flag
	;card_detail_buffer+2 card drawing begins 
	call	battle_update_card_font_color
	
	;art dump
	call	display_card_art

	;card dump
	call	card_dump
	

	ret





;a card number
;l (LCARDART_F_TILE or RCARDART_F_TILE)
;Updates card_detail_buffer with the 'a' card
print_card:
	;Left or right?
	push	af
	push	hl


	;set screen update flag
	ld		a,l
	cp		LCARDART_F_TILE
	jr		z,print_card_set_buffer_pointer_left
	ld		a,0x2						;right card dump flag 	
	jr		print_card_set_buffer_pointer_end
print_card_set_buffer_pointer_left:
	ld		a,0x1						;left card dump flag 
print_card_set_buffer_pointer_end:	
	
	
	;update display card flag in order to display new card on screen
	push	af
	ld		a,[dump_flags]
	ld		h,a
	pop		af
	or		h						;a=a or h (flag or current flags register)
	ld		[dump_flags],a
	;remember
	;0x1->left card
	;0x2->right card
	
	pop		hl
	pop		af
	
	push	hl	;(*1)


	;calc card mem right address
	ld		d,a
	ld		e,CARD_REG_SIZE
	call	multiply
	push	hl				;save despl=(card number * card reg size)

	ld		hl,cards_regs
	pop		bc				;ready for base(first card reg)+despl	
	add		hl,bc			;hl pointing at the right card mem address

	push	hl				;save right card mem address
	
	
	;save card mem address into the first two bytes of the buffer for later use
	ld		ix,card_detail_buffer
	ld		[ix],l
	ld		[ix+1],h		;tmp_buffer|shop_card_deck_buffer,tmp_buffer+1|shop_card_deck_buffer+1 has right reg card address
 
	pop		ix				;ix pointing at the right card mem address

	
	
	;update graphic art on ram
	pop		hl				;(*1);l=LCARDART_F_TILE or RCARDART_F_TILE
	ld		a,l
	push	af				;saved for later (**1)

	call	load_card_art_ram
	;card_art_buffer and card_art_buffer_color buffers updated

	pop		af	;(**1)		;a holds LCARDART_F_TILE or RCARDART_F_TILE
	

	ld		ix,card_detail_buffer
	ld		h,[ix+1]
	ld		l,[ix]			;hl card_record start


	ld		[ix],a			;card_detail_buffer tells if left or right card

	call	print_card_mem
							
	;at this point 
	;card_detail_buffer is LCARDART_F_TILE or RCARDART_F_TILE
	;card_detail_buffer+1 

	ret








;hl	card_record_start
;card_detail_buffer contains already LCARDART_F_TILE or RCARDART_F_TILE
;rewrites card_detail_buffer 
;card drawing begins at card_detail_buffer+2
;card_detail_buffer+1 will hold color flag for later render
print_card_mem:

	push	hl


	;redraw card from rom 
	ld		hl,card_detail_buffer
	ld		a,[hl]
	cp		LCARDART_F_TILE
	jr		z,print_card_mem_leftcard
	;right card
	ld		hl,right_blank_card
	ld		c,FONT_OFFSET_RIGHT_CARD
	jr		print_card_mem_leftorright_alreadyset	
print_card_mem_leftcard:
	ld		hl,left_blank_card
	ld		c,FONT_OFFSET_LEFT_CARD

print_card_mem_leftorright_alreadyset:
	ld		de,card_detail_buffer+2
	push	bc					;saves c for later use
	call	unpack
	pop		bc
	
	pop		iy					;card record start
	
	
	;save color flags
	ld		hl,card_detail_buffer+1
	ld		a,[iy+1]					;card flags
	ld		[hl],a						;saved on second byte of the buffer
	
	
	;write card title	
	ld		h,[iy+21]
	ld		l,[iy+20]					;hl=pointer to the title
	
	push	hl
	pop		ix							;ix title text address, src
	ld		hl,card_detail_buffer+2+11	;hl dest
										;c already set
	ld		a,2							;number of lines
	ld		b,2							;bytes to skip after every line
	
	push	bc							;save c for later use
	call	print_string0_mem_with_skip	;title
	pop		bc

	
	;write card text
	ld		h,[iy+23]
	ld		l,[iy+22]

	push	hl
	pop		ix							;ix src	
	ld		hl,card_detail_buffer+2+91	;hl dest
										;c already set
	ld		a,5							;number of lines
	ld		b,2							;bytes to skip at the end of every single line
	push	bc							;save c for later use (holds font offset)
	call	print_string0_mem_with_skip	;print txt in buffer
	pop		bc

	
	;write cost value
	ld		a,[iy+3]					;a=card cost
	ld		hl,graphics_buffer			;dest for two ascii digits
		
	call	calc_two_digits_counter_dest_hl			

	ld		hl,graphics_buffer+2
	xor		a
	ld		[hl],a						;set 0 to know when string is finished (if not it's gonna write a long line and not just two digits)
	;and send to buffer
	
	ld		hl,graphics_buffer
	push	hl
	pop		ix							;ix src	
	ld		hl,card_detail_buffer+2+141+6;hl dest
										;c already set
	ld		a,1							;number of lines
	ld		b,2							;bytes to skip at the end of every single line					;TODO: probably not needed
	call	print_string0_mem_with_skip	;print txt in buffer

	
	ret
	




;hl	dest
;a		lines
;ix	src
;returns a=0 if zero was found, a=1 if zero wasn't found
;all basic registers are modified
print_string0_mem:
print_string0_mem_line_loop:	
	ld		b,CARD_TEXT_WIDTH
	push	af	

print_string0_mem_char_loop:
	xor		a
	cp		[ix]
	jr		z,print_string0_mem_fill_0
	ld		a,[ix]
	ld		[hl],a
	inc		ix
	inc		hl
	djnz	print_string0_mem_char_loop

	pop		af
	dec		a
	jr		nz,print_string0_mem_line_loop
	jr		print_string0_mem_end

print_string0_mem_fill_0:	
	pop		af		;a=number of lines reamining to go
	ld		c,a
	push	bc		;save b (remaining chars to go)
	ld		b,0		;bc=a

	REPEAT	3
			sla		c
			rl		b
	ENDREPEAT					;d;bc=bc*8 2pow(3) ;bc=bc*8=a*8	

	pop		af		;a=old b
	dec		a
	add		a,c		;bc = a*8+(b-1)
	ld		c,a

	ld		a,0
	ld		[hl],a

	push	hl
	pop		de	
	inc		de		;de=hl+1
	ldir

	xor		a		;returns a=0 if 0 was found
	ret
print_string0_mem_end:	

	ld		a,1		;returns a=1 if 0 wasn't found
	ret



;populates background_left & right and set its tiles patterns and colors on the VRAM
;a = background number
init_background_for_new_game:
	cp		0
	jr		z,init_background_for_new_game_0
	cp		1
	jr		z,init_background_for_new_game_1
	;background 2
	ld		de,16
	jr		init_background_for_new_game_unpack
init_background_for_new_game_0:
	ld		de,0
	jr		init_background_for_new_game_unpack
init_background_for_new_game_1:
	ld		de,8
	jr		init_background_for_new_game_unpack
init_background_for_new_game_unpack:
	ld		hl,battle_background1
	add		hl,de

	push	hl
	ld		e,[hl]
	inc		hl
	ld		d,[hl]
	ex		de,hl
	ld		de,background_left
	call	unpack
	
	pop		hl
	inc		hl
	inc		hl
	push	hl
	
	ld		e,[hl]
	inc		hl
	ld		d,[hl]
	ex		de,hl
	ld		de,background_right
	call	unpack
	
	;now vram tiles
	pop		hl
	inc		hl
	inc		hl
	push	hl
	
	ld		e,[hl]
	inc		hl
	ld		d,[hl]
	ex		de,hl
	ld		de,CHRTBL+(29*8)
	call	DEPLET							;right to the VRAM
	
	;vram colors
	pop		hl
	inc		hl
	inc		hl
	
	ld		e,[hl]
	inc		hl
	ld		d,[hl]
	ex		de,hl
	ld		de,CLRTBL+(29*8)
	call	DEPLET							;right to the VRAM
	
	
	ret


;a==0 left, a==1 right
;
print_battle_background:
	push	af
	and		a
	jr		z,print_battle_background_left_buffer
	ld		hl,background_right
	ld		de,right_backgrnd_buffer
	jr		print_battle_background_buffer_set
print_battle_background_left_buffer:
	ld		hl,background_left
	ld		de,left_backgrnd_buffer
print_battle_background_buffer_set:	
	ld		bc,6*6
	ldir

	pop		af
	and		a
	jr		z,print_battle_background_left_buffer_clean
	;right clean
	ld		hl,right_backgrnd_buffer+36
	jr		print_battle_background_buffer_clean_set
print_battle_background_left_buffer_clean:
	ld		hl,left_backgrnd_buffer+36
print_battle_background_buffer_clean_set:
	xor		a
	ld		[hl],a
	ld		d,h
	ld		e,l
	inc		de
	ld		bc,66-36-1
	ldir
	
	ret


;a==0 left, a==1 right
;right_backgrnd_buffer or left_backgrnd_buffer must be ready
display_battle_background_tower_and_wall:
	
	and		a
	jr		z,display_battle_background_tower_and_wall_left
	ld		hl,right_backgrnd_buffer
	ld		de,NAMTBL+58								;right
	jr		display_battle_background_tower_and_wall_main

display_battle_background_tower_and_wall_left:
	ld		hl,left_backgrnd_buffer
	ld		de,NAMTBL+32								;left

display_battle_background_tower_and_wall_main:
	ld		b,11
display_battle_background_tower_and_wall_loop:	
	;hl set
	;de set
	push	bc
	ld		bc,6
	push	hl
	push	de
	call	0x005c
	pop		de
	pop		hl
	ld		bc,6

	add		hl,bc										;next vram line
	ex		de,hl
	ld		bc,32
	add		hl,bc
	ex		de,hl
	
	pop		bc
	djnz	display_battle_background_tower_and_wall_loop

	ret







print_towers_walls_on_background:

	ld		a,[player_tower_height]
	cp		0
	jr		z,print_towers_walls_on_background_after_ptower
	call	calc_tower_wall
	call	set_player_tower	
	call	print_tower_or_wall
print_towers_walls_on_background_after_ptower:

	ld		a,[cpu_tower_height]
	cp		0
	jr		z,print_towers_walls_on_background_after_ctower
	call	calc_tower_wall
	call	set_cpu_tower	
	call	print_tower_or_wall
print_towers_walls_on_background_after_ctower:

	ld		a,[player_wall_height]
	cp		0
	jr		z,print_towers_walls_on_background_after_pwall
	call	calc_tower_wall
	call	set_player_wall	
	call	print_tower_or_wall
print_towers_walls_on_background_after_pwall:

	ld		a,[cpu_wall_height]
	cp		0
	jr		z,print_towers_walls_on_background_after_cwall
	call	calc_tower_wall
	call	set_cpu_wall	
	call	print_tower_or_wall
print_towers_walls_on_background_after_cwall:

	ret






;a = how much high
;de = lowest position in pattern table
;tmp_buffer = 2 bytes with the drawing per floor
print_tower_or_wall:
	
	ld		hl,tmp_buffer3+4
	ld		[hl],a

	ld		a,0
print_tower_or_wall_loop:	
	push	af
	ld		hl,tmp_buffer3+4
	cp		[hl]
	jr		z,print_tower_or_wall_end
print_tower_or_wall_buffer_to_delete_end:	
	push	de
	ld		hl,tmp_buffer3
	ld		bc,2
	ldir						;de dest to calulated buffer position, hl source (tmp_buffer3 first and second byte), bc count 2

	ld		hl,6				;dest buffer 11*6 ... 6 bytes backwards shall begin previous line
	push	hl
	pop		de

	pop		hl			;hl=pattern table pos, de=6

	scf
	ccf					;carry flag = 0
	sbc		hl,de
	push	hl
	pop		de			;de=de-6

	pop		af			;get counter
	inc		a	
	cp		10		
	jr		nz,print_tower_or_wall_loop

print_tower_or_wall_end:
	pop		af
	
	;de already set
	ld		hl,tmp_buffer3+2
	ld		bc,2
	ldir
	
	ret







;a=height
calc_tower_wall:
	ld		hl,tmp_buffer3
	call	binbcd

	ld		a,[tmp_buffer3]
	;inc		a						;a = how tall tower/wall must be in squares

	ret



set_player_tower:
	ld		hl,tmp_buffer3
	ld		[hl],TOWER_FRONT_TILE
	inc		hl
	ld		[hl],TOWER_BACK_TILE	
	inc		hl
	ld		[hl],TOWER_FRONT_TOP_TILE
	inc		hl
	ld		[hl],TOWER_BACK_TOP_TILE			;buffer has the right two patterns
	
	ld		de,left_backgrnd_buffer+(10*6) 	;dest pointer to buffer (tower/wall base)

	ret


set_cpu_tower:
	ld		hl,tmp_buffer3
	ld		[hl],TOWER_FRONT_TILE
	inc		hl
	ld		[hl],TOWER_BACK_TILE
	inc		hl
	ld		[hl],TOWER_FRONT_TOP_TILE
	inc		hl
	ld		[hl],TOWER_BACK_TOP_TILE			;buffer has the right two patterns

	ld		de,right_backgrnd_buffer+64 	;dest pointer to buffer (tower/wall base)

	ret


set_player_wall:
	ld		hl,tmp_buffer3
	ld		[hl],P_WALL_FRONT_TILE
	inc		hl
	ld		[hl],P_WALL_BACK_TILE	
	inc		hl
	ld		[hl],P_WALL_FRONT_TOP_TILE
	inc		hl
	ld		[hl],P_WALL_BACK_TOP_TILE			;buffer has the right two patterns
	
	ld		de,left_backgrnd_buffer+(10*6)+3 	;dest pointer to buffer (tower/wall base)

	ret


set_cpu_wall:
	ld		hl,tmp_buffer3
	ld		[hl],C_WALL_FRONT_TILE
	inc		hl
	ld		[hl],C_WALL_BACK_TILE	;tmp buffer has the right two patterns
	inc		hl
	ld		[hl],P_WALL_FRONT_TOP_TILE
	inc		hl
	ld		[hl],P_WALL_BACK_TOP_TILE			;buffer has the right two patterns
	
	ld		de,right_backgrnd_buffer+64-3 	;dest pointer to buffer (tower/wall base)

	ret




display_one_digits_counters:
	ld		a,[player_dungeon_value]
	call	calc_one_digit_big_counter
	ld		de,PLAYER_DUNGEON
	call	print_one_big_digit_counter

	ld		a,[cpu_dungeon_value]
	call	calc_one_digit_big_counter
	ld		de,CPU_DUNGEON
	call	print_one_big_digit_counter

	ld		a,[player_magic_value]
	call	calc_one_digit_big_counter
	ld		de,PLAYER_MAGIC
	call	print_one_big_digit_counter

	ld		a,[cpu_magic_value]
	call	calc_one_digit_big_counter
	ld		de,CPU_MAGIC
	call	print_one_big_digit_counter

	ld		a,[player_quarry_value]
	call	calc_one_digit_big_counter
	ld		de,PLAYER_QUARRY
	call	print_one_big_digit_counter

	ld		a,[cpu_quarry_value]
	call	calc_one_digit_big_counter
	ld		de,CPU_QUARRY
	call	print_one_big_digit_counter

	ret


;a holds the numer to display (<10)
;gets ready tmp_buffer (4 bytes)
calc_one_digit_big_counter:
	cp		10			
	ret		nc			;a>10
	ret		z			;a==10

;offset = a * 4 = a << 2
	sla		a
	sla		a
	add		BIG_NUMBER_0	;offset+base

	ld		hl,tmp_buffer3
	REPEAT	3
	ld		[hl],a
	inc		hl
	inc		a
	ENDREPEAT
	ld		[hl],a			;4 times	

	ret


;tmp_buffer must contain 2x2 values
;de init pattern table position
print_one_big_digit_counter:
	push	de

	ld		hl,tmp_buffer3
	ld		bc,2	
	call 	0x005c
;TODO: review if de & hl have the right value

	ld		hl,32

	pop		de
	add		hl,de			
	push	hl
	pop		de				;de=de+32

	ld		hl,tmp_buffer3+2	;hl=hl+2
	ld		bc,2
	call 	0x005c

	ret	


display_two_digits_counters:

	ld		a,[player_tower_height]
	call	calc_two_digits_counter
	ld		de,PLAYER_TOWER
	call	print_two_digit_counter

	ld		a,[player_wall_height]
	call	calc_two_digits_counter
	ld		de,PLAYER_WALL
	call	print_two_digit_counter

	ld		a,[cpu_tower_height]
	call	calc_two_digits_counter
	ld		de,CPU_TOWER
	call	print_two_digit_counter

	ld		a,[cpu_wall_height]
	call	calc_two_digits_counter
	ld		de,CPU_WALL
	call	print_two_digit_counter

	ld		a,[player_foes_value]
	call	calc_two_digits_counter
	ld		de,PLAYER_FOES
	call	print_two_digit_counter

	ld		a,[cpu_foes_value]
	call	calc_two_digits_counter
	ld		de,CPU_FOES
	call	print_two_digit_counter

	ld		a,[player_gems_value]
	call	calc_two_digits_counter
	ld		de,PLAYER_GEMS
	call	print_two_digit_counter

	ld		a,[cpu_gems_value]
	call	calc_two_digits_counter
	ld		de,CPU_GEMS
	call	print_two_digit_counter

	ld		a,[player_bricks_value]
	call	calc_two_digits_counter
	ld		de,PLAYER_BRICKS
	call	print_two_digit_counter

	ld		a,[cpu_bricks_value]
	call	calc_two_digits_counter
	ld		de,CPU_BRICKS
	call	print_two_digit_counter

	ret



;tmp_buffer must contain ascii codes
;de pattern table position
print_two_digit_counter:
	ld		hl,tmp_buffer3
	ld		bc,2

	call 	0x005c

	ret



;a contains the value (never higher than 99)
;tmp_buffer bytes 0 and 1 holds ascii values to be printed
calc_two_digits_counter:
	
	ld		hl,tmp_buffer3
	call	calc_two_digits_counter_dest_hl
	ret






;a contains the value (never higher than 99)
;hl pointer to leave counter text
;returns buffer pointed by hl & hl+1 with ascii digits for number 
calc_two_digits_counter_dest_hl:
	cp		100			
	ret		nc			;a>100
	ret		z			;a==100
		
	push	hl			;saving hl for later
	call	binbcd

	pop		hl			;restore hl
	ld		a,ASC_0
	add		a,[hl]
	ld		[hl],a

	inc		hl

	ld		a,ASC_0
	add		a,[hl]
	ld		[hl],a		

	ret








;tmp_buffer must contain ascii codes
;hl src
;de pattern table position
print_three_digit_counter:
	;ld		hl,tmp_buffer
	ld		bc,3

	call 	0x005c

	ret




;a contains the value (max 255 obviously)
;hl address dest buffer
;tmp_buffer bytes 0 1 & 2 holds ascii values to be printed
calc_three_digits_counter:
	;cp		100			
	;ret		nc			;a>100
	;ret		z			;a==100
		
	;ld		hl,tmp_buffer
	push	hl
	call	binbcd_3digits
;[tmp_buffer]=first digit, [tmp_buffer+1]=second digit, [tmp_buffer+2]=third digit
	;ld		hl,tmp_buffer
	pop		hl
	ld		a,ASC_0
	add		a,[hl]
	ld		[hl],a

	inc		hl

	ld		a,ASC_0
	add		a,[hl]
	ld		[hl],a		

	inc		hl

	ld		a,ASC_0
	add		a,[hl]
	ld		[hl],a		

	ret



	
	
	
	
;main menu code	
;

main_menu_display_options:

	IF DEMO_MODE==1
	ld		hl,demo_label			
	ld		de,NAMTBL+MMENU_FIRST_OPTION_START+(32*11)-6
	ld		bc,18
	call	0x005c
	ENDIF

	;options
	ld		hl,main_menu_option0_txt			
	ld		de,NAMTBL+MMENU_FIRST_OPTION_START
	ld		bc,5
	call	0x005c

	ld		hl,main_menu_option1_txt			
	ld		de,NAMTBL+MMENU_FIRST_OPTION_START+(32*2)
	ld		bc,4
	call	0x005c
	
	ld		hl,main_menu_option2_txt			
	ld		de,NAMTBL+MMENU_FIRST_OPTION_START+(32*4)
	ld		bc,8
	call	0x005c
	
	ld		hl,main_menu_option3_txt			
	ld		de,NAMTBL+MMENU_FIRST_OPTION_START+(32*6)
	ld		bc,4
	call	0x005c
	
	ld		hl,main_menu_option4_txt			
	ld		de,NAMTBL+MMENU_FIRST_OPTION_START+(32*8)
	ld		bc,4
	call	0x005c

	ld		hl,main_menu_credits_txt			
	ld		de,NAMTBL+MMENU_FIRST_OPTION_START+(32*13)-9
	ld		bc,23
	call	0x005c


	ret
	
	

init_main_menu:
	
	call	0x41
	
	call	PLAYER_OFF
	
	call	cls_screen
	call	load_battle_patterns
	;extra graphics here
	
	
	call	main_menu_display_options
	
	call	load_logo
	call	display_logo
	
	
		
	ld		a,5
	call	mmenu_display_cursor
	
	ld		a,TUNE_MMENU
	call	start_music
	
	call	0x44
	
	ret
	


init_save_load_menu:
	
	;options
	ld		hl,main_menu_save_load_tape			
	ld		de,NAMTBL+MMENU_FIRST_OPTION_START
	ld		bc,4
	call	0x005c

	ld		hl,main_menu_save_load_sram			
	ld		de,NAMTBL+MMENU_FIRST_OPTION_START+(32*2)
	ld		bc,9
	call	0x005c

	ld		hl,main_menu_save_load_back			
	ld		de,NAMTBL+MMENU_FIRST_OPTION_START+(32*4)
	ld		bc,4
	call	0x005c

	ld		a,3
	call	mmenu_display_cursor
	
	ret
	
	
	
;TODO: /////////////this is temporary code. Final version will NEVER be like this	
;a = max number of items for this menu
;af,hl,bc,de modified	
mmenu_display_cursor:
	
	;delete all posible positions
	ld		b,a
	;ld		b,5
	ld		hl,NAMTBL+MMENU_CURSOR_START
	ld		de,63
mmenu_display_cursor_loop:
	ld		a,0
	call	0x004d

	inc		hl
	ld		a,0
	call	0x004d
	
	add		hl,de
	djnz	mmenu_display_cursor_loop

	;calculate de reg value (dest at vram) based on MMENU_CURSOR_START and mmenu_option
	;de=MMENU_CURSOR_START + (64*mmenu_position)
	ld		h,0
	ld		a,[mmenu_option]
	ld		l,a								;hl=mmenu_position
	REPEAT	6
			sla		l
			rl		h
	ENDREPEAT
											;hl=mmenu_position*64
	ld		de,NAMTBL+MMENU_CURSOR_START
	add		hl,de
	
	ex		de,hl	
	ld		hl,main_menu_cursor
	ld		bc,2
	call	0x005c

	ret


;/////////////this is temporary code. Final version will NEVER be like this END
	
	
	
;main menu code END	
	
	


;2 digits version
;a value
;hl points at two position bytes on memory 

binbcd:
	ld		b,10
	call	bcd
	ld		[hl],a
	ret

bcd:
	ld		[hl],0xff
bcd_store:
	inc		[hl]
	sub		b
	jr		nc,bcd_store
	add		a,b
	inc		hl

	ret
	
	

; it was taken from :
; http://read.pudn.com/downloads178/sourcecode/others/826204/bin2bcd.asm__.htm
;a 		value
;hl		points at two position bytes on memory
binbcd_3digits: 
	ld		b,100  	
	call	bcd  	
	ld		b,10  	
	call	bcd  	
	ld		[hl],a	
	ret
 




;16 bit decimal to string
;just from 0-999 version
;original from http://baze.au.com/misc/z80bits.html#5.1
;adapted by bitvision
;Input: HL = number to convert, DE = location of ASCII string
;Output: ASCII string at (DE)
Num2Dec:
;	ld		bc,-10000
;	call	Num1
;	ld		bc,-1000
;	call	Num1
	ld		bc,-100
	call	Num1
	ld		c,-10
	call	Num1
	ld		c,b

Num1:
	ld		a,'0'-1
Num2:
	inc		a
	add		hl,bc
	jr		c,Num2
	sbc		hl,bc

	ld		[de],a
	inc		de
	ret





;TODO: its common with load_battle_screen
shop_load_screen:

	ld		hl,shop_screen
	ld		de,graphics_buffer
	call	unpack
	
	ld 		hl,graphics_buffer
	ld 		de,NAMTBL
	ld 		bc,24*32
	call 	0x005c		;copy to video memory

	ret



load_battle_screen:
;TODO: it can be done in at once!
	ld		hl,battle_screen
	ld		de,graphics_buffer
	call	unpack
	
	ld 		hl,graphics_buffer
	ld 		de,NAMTBL
	ld 		bc,24*32
	call 	0x005c		;copy to video memory

	ret





load_sprites:
	ld 		hl,sprt_play					;first sprite
	ld		de,SPRTBL
	call	DEPLET
	
	ret
	
	
set_spr_attrb_4_title_scr:
	
	ld		hl,load_screen_sprites_attribs
	ld		de,SPRATR
	ld		bc,10*4
	call	0x05c
	
	ret
	


load_battle_patterns:

	ld		hl,load_battle_patterns_end
	push	hl								;ret value

	call	activate_rom_page3

	ld		bc,load_battle_patterns_ret_address_1
	ld		hl,battle_patterns
	ld		de,CHRTBL
	jp		unpack_rle_2_vram

load_battle_patterns_ret_address_1:

	ld		bc,load_battle_patterns_ret_address_2
	ld		hl,battle_patterns_colors
	ld		de,CLRTBL
	jp		unpack_rle_2_vram

load_battle_patterns_ret_address_2:

	jp		restore_ram_page3
	

load_battle_patterns_end:
/*
	ld		hl,battle_patterns_colors
	ld		de,CLRTBL
	call	DEPLET
*/



	ret




screen2x16:
;<<inicialisacion>>
; Set background and border
	ld      hl,FORCLR
	ld      [hl],15
	inc     hl
	ld      [hl],1
	inc		hl
	ld		[hl],1
	call    0x062

;click off	
	xor	a		
	ld	[CLIKSW],a
		
;- screen 2
	ld a,2
	call 0x005f


	ld b,0xe2
;ld b,0xe3 
	ld c,1
	call 0x47

	ret



cls_screen:
	ld		a,0
	ld		bc,3*256
	ld		hl,NAMTBL
	call	0x56

	ret


cls_sprites:
	ld		a,0
	ld		bc,4*64
	ld		hl,SPRATR
	call	0x56

	ret
